package cn.life.money.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.life.money.entity.MoneyAccount;
import cn.life.money.entity.MoneyAccountItem;
import cn.life.money.req.AddAccountItemReq;
import cn.life.money.req.CommonIdReq;
import cn.life.money.service.biz.MoneyBizService;
import cn.life.money.service.core.MoneyAccountItemService;
import cn.life.money.service.core.MoneyAccountService;
import cn.life.money.vo.AccountItemVO;
import cn.life.money.vo.MoneyAccountVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class MoneyBizServiceImpl implements MoneyBizService {

    @Resource
    private MoneyAccountService moneyAccountService;

    @Resource
    private MoneyAccountItemService moneyAccountItemService;

    @Override
    public List<MoneyAccountVO> getAllAccount() {
        List<MoneyAccount> accounts = moneyAccountService.list();

        if (CollUtil.isEmpty(accounts)) {
            return Collections.emptyList();
        }

        return BeanUtil.copyToList(accounts, MoneyAccountVO.class);
    }

    /**
     * 添加账户
     *
     * @author dongli
     * @since 2024/9/24 15:58
     */
    @Override
    public String addAccountItem(AddAccountItemReq req) {
        MoneyAccountItem moneyAccountItem = BeanUtil.copyProperties(req, MoneyAccountItem.class);
        moneyAccountItemService.save(moneyAccountItem);

        return "添加成功";
    }

    /**
     * 获取账户的详细信息
     *
     * @author dongli
     * @since 2024/9/24 16:17
     */
    @Override
    public AccountItemVO getAccountItem(CommonIdReq req) {
        List<MoneyAccountItem> accountItems = moneyAccountItemService.list(Wrappers.lambdaQuery(MoneyAccountItem.class)
                .eq(MoneyAccountItem::getAccountId, req.getId()));

        if (CollUtil.isEmpty(accountItems)) {
            return null;
        }

        List<AccountItemVO.AccountItem> items = accountItems.parallelStream()
                .map(x -> BeanUtil.copyProperties(x, AccountItemVO.AccountItem.class))
                .collect(Collectors.toList());
        
        BigDecimal moneySum = BigDecimal.ZERO;
        BigDecimal inMoneySum = BigDecimal.ZERO;
        BigDecimal outMoneySum = BigDecimal.ZERO;
        for (AccountItemVO.AccountItem item : items) {
            BigDecimal money = Optional.ofNullable(item.getMoney()).orElse(BigDecimal.ZERO);
            BigDecimal inMoney = Optional.ofNullable(item.getInMoney()).orElse(BigDecimal.ZERO);
            BigDecimal outMoney = Optional.ofNullable(item.getOutMoney()).orElse(BigDecimal.ZERO);
            
            moneySum = moneySum.add(money);
            inMoneySum = inMoneySum.add(inMoney);
            outMoneySum = outMoneySum.add(outMoney);
        }

        AccountItemVO vo = new AccountItemVO();
        vo.setMoneySum(moneySum);
        vo.setInMoneySum(inMoneySum);
        vo.setOutMoneySum(outMoneySum);
        vo.setAccountItems(items);

        return vo;
    }
}
